<?php

namespace app\admin\controller\wanlshop;

use app\common\controller\Backend;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 店铺运费模板
 *
 * @icon fa fa-circle-o
 */
class Freight extends Backend
{
    
    /**
     * Freight模型对象
     * @var \app\admin\model\wanlshop\Freight
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();

        $this->model = new \app\admin\model\wanlshop\Freight;
        // 判断地区表是否存在
        if(!db()->query('SHOW TABLES LIKE '."'".config('database.prefix')."area'")) {
            $this->error(__('地区表不存在，请安装开发示例插件后卸载即可'));
        }
        // 获取地址
        $this->area = collection(model('app\common\model\Area')->where('level','in',[1,2])->field('id,pid,name,level')->select())->toArray();
        $this->assignconfig('area', $this->getChild(0));
        $this->view->assign("deliveryList", $this->model->getDeliveryList());
        $this->view->assign("isdeliveryList", $this->model->getIsdeliveryList());
        $this->view->assign("valuationList", $this->model->getValuationList());
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("typeList", $this->model->getTypeList());
    }
    
    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->with(['shop'])
                    ->where($where)
                    ->where(['shop_id'=>$this->shop->id])
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->with(['shop'])
                    ->where($where)
                    ->where(['shop_id'=>$this->shop->id])
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $row) {
                
                $row->getRelation('shop')->visible(['shopname']);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if($params['isdelivery'] == 0 && empty($params['rule'])){
                $this->error(__('自定义运费，必须添加配送区域！！'));
            }
            if ($params) {
                $params['shop_id'] = $this->shop->id;
                $result = false;
                Db::startTrans();
                try {
                    $this->model->shop_id = $params['shop_id'];
                    $this->model->name = $params['name'];
                    $this->model->name_en = $params['name_en'];
                    $this->model->type = $params['type'];
                    $this->model->delivery = $params['delivery'] ?? 5;
                    $this->model->isdelivery = $params['isdelivery'];
                    if($params['isdelivery'] == 0){
                        $this->model->valuation = $params['valuation'] ?? 1;
                    }
                    if($this->model->save()){
                        $result = true;
                    }
                    // 如果不包邮
                    if($params['isdelivery'] == 0){
                        // 写入模板数据
                        $list = [];
                        foreach ($params['rule']['first'] as $key => $value) {
                            $list[] = [
                                'shop_id' => $params['shop_id'],
                                'freight_id' => $this->model->id,
                                'province' => $params['rule']['province'][$key],
                                'citys' => $params['rule']['citys'][$key],
                                'first' => $params['rule']['first'][$key],
                                'first_fee' => $params['rule']['first_fee'][$key],
                                'additional' => $params['rule']['additional'][$key],
                                'additional_fee' => $params['rule']['additional_fee'][$key]
                            ];
                        }
                        if(!model('app\index\model\wanlshop\ShopFreightData')->allowField(true)->saveAll($list)){
                            $result = false;
                        }
                    }
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($row['shop_id'] !=$this->shop->id) {
            $this->error(__('You have no permission'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $result = false;
                $results = false;
                Db::startTrans();
                try {
                    $result = $row->allowField(true)->save($params);
                    // 如果不包邮写入数据，否则删除
                    if($params['isdelivery'] == 0){
                        // 删除原来数据
                        model('app\index\model\wanlshop\ShopFreightData')
                            ->where('freight_id','in',$ids)
                            ->delete();
                        // 生成新数据
                        $list = [];
                        foreach ($params['rule']['first'] as $key => $value) {
                            $list[] = [
                                'shop_id' => $row['shop_id'],
                                'freight_id' => $ids,
                                'province' => $params['rule']['province'][$key],
                                'citys' => $params['rule']['citys'][$key],
                                'first' => $params['rule']['first'][$key],
                                'first_fee' => $params['rule']['first_fee'][$key],
                                'additional' => $params['rule']['additional'][$key],
                                'additional_fee' => $params['rule']['additional_fee'][$key]
                            ];
                        }
                        $results = model('app\index\model\wanlshop\ShopFreightData')->allowField(true)->saveAll($list);
                    }else{
                        // 删除原来数据
                        model('app\index\model\wanlshop\ShopFreightData')
                            ->where('freight_id','in',$ids)
                            ->delete();
                    }
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false || $results !== false ) {
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->assignconfig('data', collection($row->freightdata)->toArray());
        $this->assignconfig('valuation', $row['valuation']);
        $this->assignconfig('isdelivery', $row['isdelivery']);
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids) {
            $pk = $this->model->getPk();
            $this->model->where('shop_id', '=', $this->shop->id);
            $list = $this->model->where($pk, 'in', $ids)->select();
            $count = 0;
            Db::startTrans();
            try {
                foreach ($list as $value) {
                    $count += $value->delete();
                    // 如果不包邮
                    if($value['isdelivery'] == 0){
                        // 删除模板数据
                        model('app\index\model\wanlshop\ShopFreightData')
                            ->where('freight_id','in',$value['id'])
                            ->delete();
                    }
                }
                Db::commit();
            } catch (PDOException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($count) {
                $this->success();
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }


    /**
     * 内部得到子级数组方法，开发者不要外部调用
     * @param int
     * @return array
     */
    public function getChild($myid)
    {
        $newarr = [];
        foreach ($this->area as $key => $value) {
            if (!isset($value['id'])) {
                continue;
            }
            if ($value['pid'] == $myid) {
                $newarr[$value['id']] = $value;
                $newarr[$value['id']]['city'] = $this->getChild($value['id']);
            }
        }
        return $newarr;
    }
}
